package com.cn.shop.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Logger;

/**
 * MD5加密工具类
 *
 * @author suhao
 * @time 2017.09.18
 * @email suhao755@gmail.com
 */
public class MD5Util {
    static MessageDigest MD5 = null;
    private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    static {
        try {
            MD5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException ne) {
            ne.printStackTrace();
        }
    }
    /*获取md5值*/
    public static String getStringMD5(String str) {
        StringBuilder sb = new StringBuilder();
        try {
            byte[] data = str.getBytes("utf-8");
            MessageDigest MD5 = MessageDigest.getInstance("MD5");
            MD5.update(data);
            data = MD5.digest();
            for (int i = 0; i < data.length; i++) {
                sb.append(HEX_DIGITS[(data[i] & 0xf0) >> 4] + "" + HEX_DIGITS[data[i] & 0xf]);
            }
        } catch (Exception e) {
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        final Logger logger = Logger.getLogger("com.cn.shop.utils");
        String md5 = "";
        md5 = getStringMD5("123456");
        logger.info("加密信息"+md5);
    }
}